package com.lly.songhigh.fragment;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lly.songhigh.R;
import com.lly.songhigh.adapter.RadioStationClassifyAdapter;
import com.lly.songhigh.constant.StringResouce;
import com.lly.songhigh.constant.URL;
import com.lly.songhigh.entity.MusicInfo;
import com.lly.songhigh.entity.RadioStation;
import com.lly.songhigh.entity.RadioStationPlay;
import com.lly.songhigh.eventbus.MyEvent;
import com.lly.songhigh.volley.StringRequestUtf;
import com.lly.songhigh.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by 李新伟 on 15/11/6.
 * 电台分类Fragment
 */
public class RadioStationClassifyFragment extends BaseFragment implements AdapterView.OnItemClickListener {

    private View view;
    private ListView radio_station_list_view;
    private ArrayList<RadioStation.DataEntity.DataInEntity> dataEntities;
    private VolleySingleton volleySingleton;
    private List<RadioStationPlay.DataEntity> radioEntities;
    private List<MusicInfo> musicInfos;

    public static BaseFragment newInstance(List<RadioStation.DataEntity.DataInEntity> dataEntities){
        RadioStationClassifyFragment fragment = new RadioStationClassifyFragment();
        Bundle arg = new Bundle();
        arg.putParcelableArrayList("datas", (ArrayList<? extends Parcelable>) dataEntities);
        fragment.setArguments(arg);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fr_radion_classify, null);
        initView(view);
        return view;
    }

    private void initView(View view) {
        radio_station_list_view = (ListView) view.findViewById(R.id.radio_station_list_view);
        radio_station_list_view.setOnItemClickListener(this);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance();
        initData();
    }

    private void initData() {
        dataEntities = getArguments().getParcelableArrayList("datas");
        RadioStationClassifyAdapter adapter = new RadioStationClassifyAdapter(getActivity(), dataEntities);
        radio_station_list_view.setAdapter(adapter);
    }

    /**
     * 电台播放
     */
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        RadioStation.DataEntity.DataInEntity dataInEntity = (RadioStation.DataEntity.DataInEntity) parent.getItemAtPosition(position);
        musicInfos = new ArrayList<>();
        int tag_id = dataInEntity.getTag_id();
        String url = URL.RADIO_PLAY_START + tag_id + URL.RADIO_PLAY_END;
        StringRequestUtf stringRequestUtf = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                RadioStationPlay radioStationPlay = gson.fromJson(response, RadioStationPlay.class);
                radioEntities = radioStationPlay.getData();
                for (RadioStationPlay.DataEntity radioEntity : radioEntities) {
                    MusicInfo musicInfo = new MusicInfo();
                    musicInfo.setUrl(radioEntity.getUrlList().get(0).getUrl());
                    musicInfo.setDuration(radioEntity.getUrlList().get(0).getDuration());
                    musicInfo.setSingerId(radioEntity.getSingerId());
                    musicInfo.setSingerName(radioEntity.getSingerName());
                    musicInfo.setSongId(radioEntity.getSongId());
                    musicInfo.setSongName(radioEntity.getName());
                    musicInfo.setAlbum(radioEntity.getAlbumName());
                    musicInfo.setResouce(StringResouce.RESOUCE_NET);
                    musicInfos.add(musicInfo);
                }
                EventBus.getDefault().post(new MyEvent.DeliverData(musicInfos, radioEntities.get(0).getName()));
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequestUtf);
    }
}
